<template>
	<view>
		<view class="feedback-title">意见反馈：</view>
		<view class="feedback-content">
			<textarea class="feedback-textarea" placeholder="请输入您要反馈的内容" v-model="content" />
			</view>
		<view class="feedback-title">反馈图片：</view>
		<view class="feedback-image-box">
			<view class="feedback-image-item" v-for="(item,index) in imgList" :key="index" >
				<view class="close-icon" @click="delImg(index)">
					<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
				</view>
				<view class="image-box">
					<image :src="item.url" mode="aspectFill"></image>
				</view>
			</view>
			<view  class="feedback-image-item" @click="addImg" v-if="imgList.length < 5">
				<view class="image-box">
					<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
				</view>
			</view>
		</view>
		<button class="feedback-button" type="primary" @click="submit">提交反馈意见</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
			imgList: []
			};
		},
		methods:{
			delImg(index) {
				this.imgList.splice(index,1)
			},
		addImg() {
				const count = 5 - this.imgList.length
				uni.chooseImage({
					count:5,
					success: (res) => {
						uni.showLoading({
							title:'加载中'
						})
						console.log(res);
						const tempFilePaths = res.tempFilePaths
						const tempFiles = res.tempFiles
						tempFilePaths.forEach((item,index) => {
							console.log(item);
							if(index < count) {
								this.imgList.push({
									url: item,
									name: tempFiles[index].name
								})
							}
						})
						uni.hideLoading()
						uni.showToast({
							title: '上传图片成功'
						})
					},
					fail: () => {
						uni.showToast({
							title:'上传图片失败',
							icon:'none'
						})
					}
				})
			},
		async submit() {
			// if(!this.content.trim()) {
			// 	uni.showToast({
			// 		title:"请输入反馈内容",
			// 		icon:'none'
			// 	})
			// 	return
			// }
			// if(this.imgList.length === 0) {
			// 	uni.showToast({
			// 		title:"请上传反馈图片",
			// 		icon:'none'
			// 	})
			// 	return
			// }
			let imgPath = []
			for(let i = 0; i < this.imgList.length; i++) {
				const files = this.imgList[i]
				 files  = await this.uploadImage(files)
				 imgPath.push(files)
			}
			this.updateFeedBack({
				content: this.content,
				feedbackImages: imgPath
			})
		},
		async uploadImage(files) {
			const result = 	await uniCloud.uploadFile({
					filePath:files.url,
					cloudPath: files.name
				})
				return result.fileID
			},
		updateFeedBack({content,feedbackImages}) {
			uni.showLoading()
			this.$api.update_feedback({
				content,
				feedbackImages
			}).then(res => {
				uni.hideLoading()
				uni.showToast({
					title:'反馈成功',
					icon: 'none'
				})
				this.content = ''
				this.imgList = []
				setTimeout(() => {
					uni.switchTab({
						url:'/pages/tabbar/my/my'
					})
				},1500)
			}).catch(() => {
				uni.hideLoading()
				uni.showToast({
					title:'反馈失败',
					icon: 'none'
				})
			})
		}
	},
	}
</script>

<style lang="scss">
	.feedback-title {
		margin: 15px;
		margin-bottom: 0;
		font-size: 14px;
		color: #666;
	}
	.feedback-content{
		margin: 15px;
		padding: 10px;
		box-sizing: border-box;
		border: 1px #eee solid;
		.feedback-textarea{
			font-size: 12px;
			width: 100%;
			height: 100px;
		}
	}
	.feedback-image-box{
		display: flex;
		flex-wrap: wrap;
		padding: 10px;
		.feedback-image-item{
			position: relative;
			width: 33.33%;
			height: 0;
			padding-top: 33.33%;
			box-sizing: border-box;	
			.close-icon {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				right: 0;
				top: 0;
				width: 22px;
				height: 22px;
				border-radius: 50%;
				background-color: #ff5a5f;
				z-index: 2;
			}
			.image-box {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 5px;
				left: 5px;
				bottom: 5px;
				right: 5px;
				border: 1px #eee solid;
				border-radius: 5px;
				overflow: hidden;
				image{
					width: 100%;
					height: 100%;
				}
			}
			
		}
	}
	.feedback-button{
		margin: 0 15px;
		background-color: $navbar-base-color;
	}
</style>
